package com.aim.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author AIM
 * @Des Rdis配置工具
 * @DATE 2022/5/24
 */
@Configuration
public class RedisConfig {

	@Bean
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
		//关联
		template.setConnectionFactory(factory);
		// jackson序列化所有的类
		Jackson2JsonRedisSerializer Jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		// jackson序列化的一些配置
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance);
		Jackson2JsonRedisSerializer.setObjectMapper(om);
		// String的序列化
		StringRedisSerializer stringSerializer = new StringRedisSerializer();
		//将我们的key采用String的序列化方式
		template.setKeySerializer(stringSerializer);
		//将我们的hash的key也采用String的序列化方式
		template.setHashKeySerializer(stringSerializer);
		//value采用jackson序列化方式
		template.setValueSerializer(Jackson2JsonRedisSerializer);
		//hash的value也采用jackson序列化方式
		template.setHashValueSerializer(Jackson2JsonRedisSerializer);

		template.afterPropertiesSet();
		return template;
	}

}
